package com.alatus.task;

import com.alatus.NetCrmServerApplication;
import com.alatus.model.TDicType;
import com.alatus.model.TDicValue;
import com.alatus.result.DicEnum;
import com.alatus.service.ActivityService;
import com.alatus.service.DicTypeService;
import com.alatus.service.ProductService;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@EnableScheduling//开启定时任务
@Component
public class DataTask {
    @Resource
    private DicTypeService dicTypeService;
    @Resource
    private ProductService productService;

    @Resource
    private ActivityService activityService;

    @Scheduled(fixedDelayString = "${project.task.delay}",zone = "Asia/Shanghai",timeUnit = TimeUnit.MILLISECONDS,initialDelay = 1000)
    public void task(){
        List<TDicType> tDicTypeList = dicTypeService.loadAllDicData();
        tDicTypeList.forEach(tDicType -> {
            String typeCode = tDicType.getTypeCode();
            List<TDicValue> valueList = tDicType.getDicValueList();
            NetCrmServerApplication.cacheMap.put(typeCode,valueList);
        });
        NetCrmServerApplication.cacheMap.put(DicEnum.PRODUCT.getCode(),productService.getAllOnSaleProduct());
//        查询所属活动
        NetCrmServerApplication.cacheMap.put(DicEnum.ACTIVITY.getCode(),activityService.getOnGoingActivities());
    }
}
